//
//  JUFNHomeCellModel.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/10.
//  Copyright © 2017年 陈俊峰. All rights reserved.
//

import UIKit

class JUFNHomeCellModel: NSObject {
    
    //记录头像的URL地址
    var headURL : URL?
    //记录头像会员等级的image
    var memberShipImage : UIImage?
    //储存型属性,保存数据,做到更加高效
    var rankImage : UIImage?
    
    var images_url:[JUFNPicUrlsModel]?
    
    //日期字符串的处理
    var creatDate:String?{
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        //在真机上必须要设置时间信息本地化,否则转换失败
        dateFormatter.locale = Locale(identifier: "en")
        let currentDate = Date()
        guard let createdDate = dateFormatter.date(from: userModel?.created_at ?? "") else{
            return "超越自己,超越时间"
        }
        
        //创建日历对象,比较是否是今天
        let calendar = NSCalendar.current
        let resComponents = calendar.dateComponents([.year,.day], from: createdDate, to: currentDate)
        //今年
        if resComponents.year == 0 {
            
            if calendar.isDateInToday(createdDate) {
                //0 -- 60s "刚刚"
                let daltaInter = currentDate.timeIntervalSince(createdDate)
                if daltaInter < 60 {
                    return "刚刚"
                }else if daltaInter < 3600 && daltaInter >= 60{
                    //60 -- 3600s "多少分钟以前"
                    return "\(Int(daltaInter/60))分钟以前"
                }else{
                    //3600 -- 24小时以内,"xx小时以前"
                    return "\(Int(daltaInter/3600))小时以前"
                }
            }else if calendar.isDateInYesterday(createdDate){
                //昨天
                dateFormatter.dateFormat = "昨天 HH:mm"
                return dateFormatter.string(from: createdDate)
            }else{
                
                //早于昨天
                dateFormatter.dateFormat = "MM:dd:HH:mm"
                return dateFormatter.string(from: createdDate)
            }
            
        }else{
            //非今年
            dateFormatter.dateFormat = "yyyy:MM:dd"
            return dateFormatter.string(from: createdDate)
        }
        
    }
    
    //来源信息的处理
    var sourceText:String?
    
    var userModel : JUFNUserStatus?{
        didSet{
            headURL = URL(string: userModel?.user?.avatar_large ?? "")
            memberShipImage = getMembershipImage(verified_type: userModel?.user?.mbrank ?? 0)
            rankImage = getImage(rankLevel: userModel?.user?.verified_type ?? 0)
            sourceText = handerSourceInfo(sourceStr: userModel?.source ?? "")
            if userModel?.retweeted_status == nil {
                images_url = userModel?.pic_urls
            }else{
                images_url = userModel?.retweeted_status?.pic_urls
            }
        }
    }
    private func handerSourceInfo(sourceStr:String) -> String {
        
        
        let startStr = ">"
        let endStr = "</a>"
        guard let strRange = sourceStr.range(of: startStr),
            let endRange = sourceStr.range(of: endStr) else{
                return "火星情报局的吧"
        }
        //不可以运算为可选类型的,请用guard拦截
        let range = strRange.upperBound..<endRange.lowerBound
        
        return sourceStr.substring(with: range)
    }
    
    //持有一个模型
    private func getImage(rankLevel:Int) -> UIImage? {
        ///认证类型: -1：没有认证，0：认证用户，2,3,5: 企业认证，220: 达人(草根)
        if rankLevel == 2 || rankLevel == 3 || rankLevel == 5 {
            return UIImage(named: "avatar_enterprise_vip")!
        }
        if rankLevel == 220 {
            return UIImage(named:"avatar_grassroot")!
        }
        if rankLevel == 0{
            return UIImage(named: "avatar_vip")!
        }
        return nil
    }
    
    
    private func getMembershipImage(verified_type:Int) -> UIImage? {
        
        if (verified_type > 0)&&(verified_type < 7)  {
            return UIImage(named: "common_icon_membership_level\(verified_type)")
        }else{
            return UIImage(named: "common_icon_membership_expired")
        }
    }
}
